<?php

class Sidebar
{
    const VARIATIONS_COUNT = 20;
    const SIDEBAR_PANEL_DEFAULT_CLASS = 'DefaultSidebarPanel';
    protected $sidebarsInfo = null;
    
    public function __construct() 
    {
        $this->sidebarsInfo = Core::ResourceManager()->GetSidebarsInfo();
    }

    protected function GetSidebarPanelsByPageId( $pageId )
    {
        $sidebars = $this->sidebarsInfo['sidebars'];
        return isset($sidebars[$pageId]) ? $sidebars[$pageId] : $sidebars['-'];
    }

    private static function GetPanelContent( TemplateEngine $tmpl, $pageId, $panelInfo, $seed )
    {
        $className = empty($panelInfo['className']) ? self::SIDEBAR_PANEL_DEFAULT_CLASS : $panelInfo['className'];
        $sidebarClass = new $className($seed);
        return $sidebarClass->GetContent($panelInfo, $tmpl, $pageId);
    }

    public function GeneratePanel( $pageId, $panelId )
    {
        $panelInfo = $this->sidebarsInfo['panels'][$panelId];
        $className = empty($panelInfo['className']) ? self::SIDEBAR_PANEL_DEFAULT_CLASS : $panelInfo['className'];
        $seed = rand(0, self::VARIATIONS_COUNT);
        $sidebarClass = new $className($seed);
        $tmpl = new TemplateEngine( PATH_TEMPLATES."custom_sidebar/" );
        return $sidebarClass->GetContent($panelInfo, $tmpl, $pageId);
    }

    /**
     * Checks was sidebar specified for current page id.
     * @param string $pageId
     * @return bool
     */
    public function CheckDoesSidebarsExist( $pageId )
    {
        $sidebarExists = false;

        $sidebarInfos = $this->sidebarsInfo;
        $sidebars = !empty( $sidebarInfos ) && isset( $sidebarInfos['sidebars'] ) ? $sidebarInfos['sidebars'] : null;
        $sidebarExists = !is_null( $sidebars ) && array_key_exists( $pageId, $sidebars );

        return $sidebarExists;
    }
    
    protected function GetSidebarPanelsInfo(PageInfo $pageInfo)
    {
        $pageId = ( $pageInfo->IsTranslatedPage() && $this->CheckDoesSidebarsExist( $pageInfo->id ) ) 
                      ? $pageInfo->id : $pageInfo->GetOriginalPageId();

        return $this->GetSidebarPanelsByPageId($pageId);
    }
    
    protected function GetSidebarHash($sidebarPanels = null)
    {
        return Core::GetProperty("last_news_hash");
    }
    

    public function Generate( $pageId )
    {
        $sidebarPanels = $this->GetSidebarPanelsInfo(Pages::GetPageInfo( $pageId ));
        
        if ( empty( $sidebarPanels ) )
        {
            return '';
        }
        
        $hash = $this->GetSidebarHash($sidebarPanels);
        $seed = rand(0, self::VARIATIONS_COUNT);

        $cache = Core::Cache();
        $cacheId = "html/$pageId/sidebar_{$seed}_{$hash}";
        $content = $cache->Get($cacheId);
        if ( is_null($content) )
        {
            $tmpl = new TemplateEngine( PATH_TEMPLATES."custom_sidebar/" );
            $tmpl->define('space', 'sidebar_space.tpl');
            $tmpl->parse('space', 'space');
            $space = $tmpl->getGenerated();

            $content = '';
            foreach ( $sidebarPanels as $panelId )
            {
                if ( isset($this->sidebarsInfo['panels'][$panelId]) )
                {
                    $tmpl->clearAll();
                    $panelInfo = $this->sidebarsInfo['panels'][$panelId];
                    $content .= self::GetPanelContent( $tmpl, $pageId, $panelInfo, $seed ) . $space;
                }
                else
                {
                    trigger_error("Panel with id '$panelId' is not found.", E_USER_WARNING);
                }
            }

            $cache->Set($cacheId, $content);
        }
        return $content;
    }
}

?>
